const R = require('ramda')
const { Model } = require('sequelize')
const makeCoverter = (entityClass, fields) => {
  const toSequelizeModel = (entity) => {
    const data = R.pick(['id', ...fields], entity)
    return data
  }

  const toEntity = (data) => {
    if (!data) return null
    if (data instanceof Model) return entityClass.create(data.toJSON())
    return entityClass.create(data)
  }

  return {
    toDatabase: toSequelizeModel,
    toDatabaseList: R.map(toSequelizeModel),
    toEntity,
    toEntityList: R.map(toEntity),
  }
}
module.exports = {
  makeCoverter,
}
